package com.zkingsoft.actions.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.Role;
import com.zkingsoft.model.RolePwoer;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.RoleService;

/**
 * @description 角色管理action
 * @author 姜友瑶
 * @email 935090232@qq.com
 * @date 2016-06-26
 */
@Controller
@RequestMapping(value = "/admin/role")
public class RoleController extends BaseController {

	@Resource
	private RoleService currentService;
	@Resource
	private RolePwoerService pwoerService;

	/**
	 * 
	 * @Description: 分页查询角色
	 * @author:姜友瑶
	 * @param request
	 * @param role
	 * @param pageVo
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Role role, PaginationVO pageVo) {
		role.setCompanyId(getMe().getCompanyId());
		return showList(currentService, role, pageVo);
	}

	/**
	 * 
	 * @Description: 角色新增或者修改
	 * @author:姜友瑶
	 * @param role
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Role role) {
		if (role.getId() != null) {
			return modify(currentService, role, "角色");
		} else {
			return add(currentService, role, "角色");
		}
	}

	/**
	 * 
	 * @Description: 删除角色
	 * @author:姜友瑶
	 * @param keys
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 
	 * @Description: 进入角色编辑界面
	 * @author:姜友瑶
	 * @param id
	 * @param request
	 * @return 返回类型 String
	 * @date 2016年8月31日
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Role role;
		if (id != null) {
			role = currentService.findById(id);
			request.setAttribute("obj", role);
		}
		return "admin/role-form";
	}

	/**
	 * 
	 * @Description: 查询所有角色
	 * @author:姜友瑶
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(currentService, null);
	}

	/**
	 * 
	 * @Description: 查询角色所有的权限
	 * @author:姜友瑶
	 * @param rolePwoer
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/findRolePwoer")
	public @ResponseBody AjaxResult findRolePwoer(RolePwoer rolePwoer) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, pwoerService.findByModel(rolePwoer), 0);
	}

}